import {request} from '../../utils/http'

Page({
  data: {
    order_id:'',
    status:70,
    isLoadPlugin: false,
    sign_order_type:null
  },
  onLoad(query) {
    let order_id = query.order_id;
    let sign_order_type = my.getStorageSync({key: 'sign_order_type'}).data

    if (!order_id || order_id == 'undefined') {
      order_id = my.getStorageSync({key: 'sign_order_id'}).data
    }
    console.log(query,'query', order_id)
    

    this.setData({
      sign_order_type,
      order_id,
    }, () => {
      this.fetch(this.data.order_id)
    })
  },
  fetch(id) {
    request('/order/' + id, 'get').then(res => {
      if (res.code == 200) {
        this.setData({
          order: res.data
        })

        request('/shop/smid','get', {shop_id:res.data.shop.id}).then(res => {
          if (res.code == 200) {
            this.setData({
              smid:res.data.smid
            },() => {
              this.loadPlugin()
            })
          }
        })
      }
      
     
    })
  },
  /**
  *	初始化数据 - 宿主传参
  */

  loadPlugin() {
    console.log('loadPlugin')
    my.loadPlugin({
      plugin: '2021003126645865@*', // 指定要加载的插件id和版本号，为 * 时自动选择版本
      success: () => {
        // 初始化数据 - 宿主传参
        console.log('success')
        this.setData({
          isLoadPlugin: true
        })
      },
      fail: (err) => {
        console.log(err)
      }
    });
  },
  orderQuery() {
    if (!this.data.isLoadPlugin) {
      my.showToast({content: '插件加载失败'})
      return;
    }
    
    this.initData(this.data.order)
    // 跳转至插件首页
    my.navigateTo({
      url: 'dynamic-plugin://2021003126645865/index'
    })
  },
  initData(order) {
    // 引用插件实例
    const plugin = requirePlugin('dynamic-plugin://2021003126645865');
    const ext = my.getExtConfigSync()
  
    //plugin.setEnv('dev');
    
    // 查询
    plugin.init({
      queryOptions: {
         merchantPid: this.data.smid, // 必传 - 商户PID
         productNo: this.data.order.product_id, // 必传 - 产品编号
         subscriptionNo: this.data.order.order_no, //必传 - 订购编号
      }
    })
  },
  submit(){

    if (this.data.sign_order_type == 3) {
      my.navigateTo({ 
        url: '/pages/bind-bank/bind-bank?id=' + this.data.order_id + '&type=1'
      })

    } else {
      this.submitNext()
    }

  },
  submitNext(){
      console.log('1111')
      let order = this.data.order;

      const plugin = requirePlugin('dynamic-plugin://2021003126645865');
      console.log('2222')

      if (order.version == 4) {
        this.goZM4(order)
      } else {
        const ext = my.getExtConfigSync()
        const options = {
          merchantPid: this.data.smid, // 必传 - 商户PID
          productNo: order.product_id, // 必传 - 产品编号
        
          subscriptionType: order.pay_date == 1 ? 'MONTH' : 'WEEK', // 非必填, 订购类型, 默认为月卡
          productName: order.product_title, //必传 - 产品名称
          deductionAmount: order.second_amount, // 必传 - 每次扣减固定金额
          periods: order.total_stage, // 必传 - 开通总周期数
          downPayment: order.first_amount, // 非必填 - 前N期金额
          downPaymentPeriods: order.first_stage, // 非必填 -前多少期价格不一样，不传默认为1
          downPaymentDate: order.stage_list[0].date,
          outSubscriptionNo: order.out_order_no
        }
        console.log('333',options)
  
        plugin.init({
          openOptions: options
        });
        // 跳转至插件首页
        my.navigateTo({
          url: 'dynamic-plugin://2021003126645865/index'
        })
      }


  },
  goOrder(){
    my.navigateTo({ url: '../orderDetail/index?id=' + this.data.order_id + '&type=1'})
  },
  goPortal(){
    my.switchTab({
      url:'/pages/index/index'
    })
  },
  goZM4(data){
    const accountInfo = my.getAccountInfoSync();
    const targetUrl = encodeURIComponent('/pages/zhima-result/zhima-result');
    let url =`/pages/cycle-card-buy/index?spcTemplateId=${data.card_template_id}&outOrderNo=${data.out_order_no}&shopId=${data.zm_shop_id}&jumpType=merchant&appId=${ accountInfo.miniProgram.appId || ''}&targetUrl=${targetUrl}`
    if (data.card_property_id) {
      url = `${url}&propertyId=${data.card_property_id}`
    }
    console.log(url)
    // return
    my.navigateToMiniProgram({
      appId: '2021004147687320',
      path: url,
      success: () => {
        console.log('navigateToMiniProgram success');
      },
      fail: res => {
        my.alert({
          title: 'navigateToMiniProgram fail',
          content: JSON.stringify(res),
        });
      },
    });
  },
});
